//
//  LikeAnimation.m
//  分享demo
//
//  Created by pc-chtypd056 on 2018/8/7.
//  Copyright © 2018年 CTL. All rights reserved.
//

#import "LikeAnimation.h"

@interface LikeAnimation()

@end

@implementation LikeAnimation

+(instancetype)shareInstance{
    static LikeAnimation *animations = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        animations = [[self alloc]init];
    });
    return animations;
}

/**  系统touch来触发的动画*/
- (void)createAnimationWithTouch:(NSSet<UITouch *> *)touchs wihtEvent:(UIEvent *)event{
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    CGPoint point = [touch locationInView:[touch view]];
    UIImage *image = [UIImage imageNamed:@"yp_video_like"];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    imageView.image = image;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.center = point;
    
    /**  左右随机显示*/
    int leftOrRinght = arc4random()%2;
    leftOrRinght  = leftOrRinght ? leftOrRinght : -1;
    imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI/9*leftOrRinght);
    [[touch view] addSubview:imageView];
    
    /** 出现的时候回弹一下*/
    __block UIImageView *blockImageView = imageView;
    [UIView animateWithDuration:0.1 animations:^{
        blockImageView.transform = CGAffineTransformScale(blockImageView.transform, 1.2, 1.2);
    } completion:^(BOOL finished) {
        blockImageView.transform = CGAffineTransformScale(blockImageView.transform, 0.8, 0.8);
        /** 向上飘，放大，透明*/
        [self performSelector:@selector(animationTop:) withObject:blockImageView afterDelay:0.3];
    }];
}

/**  点击来触发的动画*/
- (void)createAnimationWithTap:(UITapGestureRecognizer *)tap{
    CGPoint point = [tap locationInView:[tap view]];
    UIImage *image = [UIImage imageNamed:@"yp_video_like"];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    imageView.image = image;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.center = point;
    [[tap view]addSubview:imageView];
    
    int leftOrRinght = arc4random()%2;
    leftOrRinght  = leftOrRinght ? leftOrRinght : -1;
    imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI/9*leftOrRinght);
    
    __block UIImageView *blockImageView = imageView;
    [UIView animateWithDuration:0.1 animations:^{
        blockImageView.transform = CGAffineTransformScale(blockImageView.transform, 1.2, 1.2);
    } completion:^(BOOL finished) {
        blockImageView.transform = CGAffineTransformScale(blockImageView.transform, 0.8, 0.8);
        [self performSelector:@selector(animationTop:) withObject:blockImageView afterDelay:0.3];
    }];
}

- (void)animationTop:(UIImageView *)blockImageView{
    [UIView animateWithDuration:1.0 animations:^{
        blockImageView.frame = CGRectMake(blockImageView.frame.origin.x, blockImageView.frame.origin.y - 100, blockImageView.frame.size.width, blockImageView.frame.size.height);
        blockImageView.transform = CGAffineTransformScale(blockImageView.transform, 1.8, 1.8);
        blockImageView.alpha = 0.0;
    } completion:^(BOOL finished) {
        
    }];
}


@end
